import axios from 'axios'
import qs from 'qs'
// Using application/x-www-form-urlencoded format

// By default, axios serializes JavaScript objects to JSON. To send data in the application/x-www-form-urlencoded format instead, you can use one of the following options.

// Browser

// In a browser, you can use the URLSearchParams API as follows:

// var params = new URLSearchParams();
// params.append('param1', 'value1');
// params.append('param2', 'value2');
// axios.post('/foo', params);

const xlAjax = axios.create({
  baseURL: process.env.NODE_ENV == 'development' ? '' : '',
  headers:{
    post:{
      'Content-Type':'application/x-www-form-urlencoded'
    }
  },
  timeout: 30000
  // validateStatus(status) {
  //   return status == 200
  // }
})

// xlAjax.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

const error = (promise) =>
  promise.catch(error => {
    window.Vue.$vux.toast.show({
      text:'网络异常！建议链接wifi使用！',
      position:'middle'
    })
  })

const post = (api,data,succeed) => error(xlAjax.post(api,qs.stringify(data))).then(res=>{
  if(res && res.status == 200){
      succeed(res)
  }
})

export const proList = (succeed) =>
post('jkda/goods/get-list',{},succeed)

export const proInfo = (body,succeed) =>
  post('jkda/goods/get-one', body,succeed)

export const getAddress = body => xlAjax.post('jkda/express-address/get-list', body)

export const getOrderList = (vm, to,succeed,limit=5) => {
  let now = vm.listData[to.params.type]
  return post('jkda/sale-order/order-list', {
      user_id: vm.$store.state.user_id,
      pay_status: to.params.type,
      start: now.page * limit,
      limit
    },res=>{
        succeed(res,now)
    })
}
export const getOrderInfo = (body,succeed)=> post('jkda/sale-order/order-info',body,succeed
)

export const getLogistics =(body,succeed)=> post('jkda/sale-order/order-express',body,succeed)
